package com.slt.developkitweb.controler.swagger;

import com.slt.developkitpersistent.dto.UserDto;
import com.slt.developkitpersistent.model.User;
import com.slt.developkitservice.service.Swagger2Service;
import com.slt.developkitweb.annotation.ShowAPI;
import com.sun.org.apache.bcel.internal.generic.NEW;
import io.swagger.annotations.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 *
 */
@ApiResponses({
        @ApiResponse(code = 200, message = "请求成功"),
        @ApiResponse(code = 400, message = "参数错误")
})
@ShowAPI
@Api(tags = "swagger2的接口")
@RestController
@RequestMapping("/swagger2")
public class Swagger2Controller {
    @Resource
    private Swagger2Service swagger2Service;

    /**
     * get 从url传 单个值   http://localhost:8888/swagger2/testPost1?code=1&name=2
     * get 从url传 数组     http://localhost:8888/swagger2/testPost1?code=1&name=2&friend=zhangsan&friend=lisi
     * @return
     */
    @GetMapping(value = "/testGet1")
    @ApiOperation(value = "get方法dto传值")
    public ResponseEntity<User> testGet(UserDto userDto) {
        User user = swagger2Service.getUserInfo(userDto);
        return ResponseEntity.ok(user);
    }

    /**
     * 测试get方法
     * @ApiParam 需要配合 @RequestParam 才能生效
     * @ApiParam(required = true) 只在swagger请求页面生效，换成apifox就成了非必传
     * @RequestParam(name = "name", defaultValue = "slt")  name ：参数名
     */
    @GetMapping(value = "/testGet2")
    @ApiOperation(value = "get方法url传值")
    @ApiParam(name = "name",value = "姓名",required = true)
    public ResponseEntity<User> testGet2(@RequestParam(name = "name", defaultValue = "slt") String name) {
        System.out.println("name:" + name);
        User user = swagger2Service.getUserInfo(new UserDto());
        return ResponseEntity.ok(user);
    }

    @GetMapping(value = "/testGet3")
    @ApiOperation(value = "get方法URL传值2")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value="姓名",required=true,paramType="query"),
            @ApiImplicitParam(name="code",value="用户编码",required=true,paramType="query")
    })
    public ResponseEntity<User> testGet3(@RequestParam(name = "name") String name,
                                         @RequestParam(name = "code") String code) {
        System.out.println("name:" + name);
        User user = swagger2Service.getUserInfo(new UserDto());
        return ResponseEntity.ok(user);
    }
    /**
     * restful 风格
     * path 参数： id
     */
    @ApiOperation("get方法restful传值")
    @GetMapping(value = "/testGet4/{id}")
    public ResponseEntity<User> testGet4(@ApiParam(name = "id", value = "用户ID", required = true) @PathVariable String id) {
        System.out.println("id:" + id);
        User user = swagger2Service.getUserInfo(new UserDto());
        return ResponseEntity.ok(user);
    }


    /**
     * get 传值方式，post都支持
     * post 从url传 单个值   http://localhost:8888/swagger2/testPost1?code=1&name=2
     * post 从url传 数组     http://localhost:8888/swagger2/testPost1?code=1&name=2&friend=zhangsan&friend=lisi
     * @return
     */
    @PostMapping(value = "/testPost1")
    @ApiOperation(value = "post方法url传值")
    public ResponseEntity<User> testPost1(UserDto userDto) {
        User user = swagger2Service.getUserInfo(userDto);
        return ResponseEntity.ok(user);
    }


    /**
     * post 从body 传值
     * @return
     */
    @ApiOperation("post方法body传值")
    @PostMapping(value = "/testPost2")
    public ResponseEntity<User> testPost2(@RequestBody UserDto userDto) {
        System.out.println(userDto);
        User user = swagger2Service.getUserInfo(userDto);
        return ResponseEntity.ok(user);
    }

    /**
     * post form 表单数据传值
     * @param multipartFiles
     * @return
     */
    @ApiOperation(value = "post方法from表单传值",authorizations = {@Authorization(value = "test")})
    @PostMapping(value = "/testPost3")
    public ResponseEntity<UserDto> testPost3(UserDto userDto, MultipartFile [] multipartFiles) {
        return ResponseEntity.ok(userDto);
    }
}
